package com.breeze.components.web.filter;

import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;

/**
 * @author breeze
 */
@Slf4j
public class XssFilter implements Filter {

  @Override
  public void init(FilterConfig filterConfig) {
    log.info("Initializing XssFilter");
  }

  @Override
  public void doFilter(
      ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
      throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    // 先进行转义在把请求返回
    XssCleanRequestWrapper wrapper = new XssCleanRequestWrapper(request);
    filterChain.doFilter(wrapper, servletResponse);
  }
}
